package com.yunfei.commonbase.base;

import com.yunfei.commonbase.base.entity.Example;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.util.List;

public class BaseService <T extends BaseEntity,D extends BaseMapper<T>>{
    @Autowired
    protected D thisMapper;

    protected Class entity;

    public List<T> getListByExample(Example example,int pageNum ,int pageSize) {
        return thisMapper.getListByExample(example,pageNum,pageSize);
    }

    public T getById(Object object) {
        return thisMapper.getById(object,getEntityClass());
    }

    public int deleteByEntity(T entity) {
        return thisMapper.deleteByEntity(entity);
    }

    public int updateByExample(T entity,Example example) {
        return thisMapper.updateByExample(entity,example);
    }

    public int insertEntity(T entity){
        return thisMapper.insertEntity(entity);
    }

    private Class getEntityClass() {
        if (entity == null) {
            entity = (Class) ((ParameterizedType) ((Class) thisMapper.getClass().getGenericInterfaces()[0]).getGenericInterfaces()[0]).getActualTypeArguments()[0];
        }
        return entity;

    }

}
